﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Hardanger.Model;
using System.Security.Cryptography;


namespace Hardanger.Features.ServerFuzzer.ServerFuzzerEngine
{
	public class BasicRandomFuzzer : IServerFuzzerEngine
	{
        private int _minLength = 1;
        public int MinLength {
            get 
            {
                return _minLength;
            }
            set
            {
                _minLength = value;
            }
        }

        private int _maxLength = 1;
        public int MaxLength 
        {
            get
            {
                return _maxLength;
            }
            set
            {
                _maxLength = value;
            }
        }
        
        public BasicRandomFuzzer()
        {
            _minLength = 1;
            _maxLength = 25;

        }
        public string GetNextValue(FuzzerSettingType type)
        {
            switch (type)
            {
                case FuzzerSettingType.Numeric:
                    return GetRandomString();
                case FuzzerSettingType.String:
                    return GetRandomString();
                default:
                    throw new HardangerException("Invalid fuzzing setting type passed to GetNextValue");
            }
        }

        private String GetRandomString()
        {
            RNGCryptoServiceProvider crypto = new RNGCryptoServiceProvider();
            byte[] data = new byte[new Random().Next(_minLength,_maxLength)];
            crypto.GetNonZeroBytes(data);
            return Encoding.UTF8.GetString(data);
        }



        public bool Configurable
        {
            get { return true; }
        }

        public string Description
        {
            get { return "A basic random string generator"; }
        }

        public string Name
        {
            get { return "Basic Random Fuzzer"; }
        }
    }
}
